package com.coin.exchanges.view.user

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.widget.Toast
import com.coin.exchanges.R
import com.coin.exchanges.model.okex.response.UserRegisterRes
import com.coin.exchanges.net.RetrofitFactory
import com.coin.exchanges.presenter.TestPresenter
import com.coin.exchanges.utils.KeybordUtil
import com.coin.exchanges.utils.LCSharedPreferencesHelper
import com.coin.exchanges.utils.SpUtils
import com.coin.exchanges.view.MainActivity
import com.coin.exchanges.view.user.bean.UserLoginBean
import com.coin.libbase.presenter.BasePresenter
import com.coin.libbase.view.activity.JToolbarActivity
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_login.*
import java.util.HashMap

class LoginActivity: JToolbarActivity<TestPresenter>() {
    override fun getLayout(): Int {
        return R.layout.activity_login
    }

    override fun initIntent(intent: Intent?) {
    }

    override fun initView() {
        setToolbarBackground(R.color.white)
        title = "注册"

    }

    override fun initData() {
        val sharedPreferencesHelper = LCSharedPreferencesHelper.instance(this@LoginActivity, "register")

        if(sharedPreferencesHelper.getBoolean("isLogin")){
            startActivity(Intent(this@LoginActivity, MainActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK))

        }

        //登陆按钮
        mLoginBtn.setOnClickListener {
            KeybordUtil.closeKeybord(this)
            if(registerCheckAll(mLoginPhoneEt.text.toString().trim(),
                            mLoginPasswordEt.text.toString().trim())){
                PostNetRegisterCheckAll(mLoginPhoneEt.text.toString().trim(),
                        mLoginPasswordEt.text.toString().trim())
            }

        }
        //注册按钮
        mRegisterTv.setOnClickListener {
            startActivity(Intent(this@LoginActivity, RegisterActivity::class.java))

        }

    }
    private fun PostNetRegisterCheckAll(mobileNum: String,
                                 password: String){
        val map = HashMap<String, String>()
        map["phone"] = mobileNum
        map["password"] = password
        RetrofitFactory
                .getOkExApiService()
                .login(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<UserLoginBean> {
                    override fun onNext(mUserLoginBean: UserLoginBean) {
                      //  val s = mUserLoginBean.data[0].UserId
                        if(mUserLoginBean.result==1){
                            val sharedPreferencesHelper = LCSharedPreferencesHelper.instance(this@LoginActivity, "register")
                            sharedPreferencesHelper.putBoolean("isLogin", true)
                            sharedPreferencesHelper.putValue("UserId", mUserLoginBean.data[0].UserId)

                            SpUtils.putBookBean(this@LoginActivity,mUserLoginBean)
                            val s =SpUtils.getBookBean(this@LoginActivity).data[0].UserId
                            val s2 =SpUtils.getBookBean(this@LoginActivity).data[0].BankCard
                            startActivity(Intent(this@LoginActivity, MainActivity::class.java).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK))

                        }else{
                            Toast.makeText(this@LoginActivity, mUserLoginBean.returnval, Toast.LENGTH_LONG).show()

                        }
                    }


                    override fun onSubscribe(d: Disposable) {

                    }

                    override fun onError(e: Throwable) {

                    }

                    override fun onComplete() {

                    }
                })
    }
    private fun registerCheckAll(mobileNum: String,
                                 password: String
                                ): Boolean {


        if (TextUtils.isEmpty(mobileNum)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_LONG).show()
            return false
        }
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show()
            return false
        }

        return true
    }
}
